<?php

namespace frontend\controllers;

use Yii;
use frontend\components\Controller;
use frontend\models\AddressForm;
use frontend\models\AddressSearch;

/**
 * 用户模块接口
 * PassportController
 */
class AddressController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            '*' => ['POST'],
            'index' => ['GET'],
        ];
    }

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function rules() {
        return [
            [
                'actions' => ['index', 'create', 'edit', 'delete', 'default'],
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * 地址列表
     * @return \yii\data\ActiveDataProvider
     */
    public function actionIndex() {
        $model = new AddressSearch();
        return $model->search();
    }

    /**
     * 创建地址
     */
    public function actionCreate() {
        $model = new AddressForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('参数错误 ');
        }
        return $model->create();
    }

    /**
     * 维护地址
     */
    public function actionEdit() {
        $model = new AddressForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('参数错误 ');
        }
        return $model->edit();
    }

    /**
     * 删除地址
     */
    public function actionDelete() {
        $model = new AddressForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('参数错误 ');
        }
        return $model->delete();
    }

    /**
     * 设为默认地址
     */
    public function actionDefault() {
        $model = new AddressForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('参数错误 ');
        }
        return $model->setDefault();
    }

}
